package com.classic.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.classic.base.LazyLoadFragment;
import com.classic.duanzhi.R;
import com.classic.duanzhi.UDetailsActivity;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;

/**
 * A simple {@link Fragment} subclass.
 */
public class DetailsListFragment extends LazyLoadFragment {
    private static final String DATA_ = "DATAA";

    public boolean isOnClick = false;

    public static DetailsListFragment getFragment(String url) {
        DetailsListFragment fragment = new DetailsListFragment();
        Bundle bundle = new Bundle();
        bundle.putString(DATA_, url);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutResourceId() {
        return R.layout.fragment_details_list;
    }

    TextView mContent, mTtitle;
    RefreshLayout refreshLayout;
    LinearLayout empty;
    FrameLayout bannerContainer;

    @Override
    protected void initView() {
        mContent = rootView.findViewById(R.id.tv_content);
        refreshLayout = rootView.findViewById(R.id.refreshLayout);
        mTtitle = rootView.findViewById(R.id.tv_details_title);
        empty = rootView.findViewById(R.id.ll_empty);
        bannerContainer = rootView.findViewById(R.id.bannerContainer);
        //刷新
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
//                initRefresh(getArguments().getString(DATA_));
                initRefresh(activity.getUrl());
            }
        });
    }

    void initRefresh(final String url) {
        new Thread() {
            @Override
            public void run() {
                Document doc;
                try {
                    doc = Jsoup.connect(url).get();
                    final Elements elementsByClass = doc.getElementsByClass("title");
                    final Elements info = doc.getElementsByClass("content");
                    final Elements upnext = doc.getElementsByClass("upnext");
                    Elements attr = upnext.select("a");
                    if(attr.size()!=0){
                        if (!TextUtils.isEmpty(attr.get(1).attr("href"))){
                            activity.addUrl(attr.get(1).attr("href"));
                        }
                    }

                    final String replace = info.text().replace(" ", "\n    ");
                    assert null != getActivity();
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            setContent(replace, elementsByClass.text());
                            empty.setVisibility(View.GONE);
                        }
                    });
                } catch (IOException e) {
                    assert null != getActivity();
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            refreshLayout.finishRefresh();
                            empty.setVisibility(View.VISIBLE);
                            isOnClick = true;
                        }
                    });
                }
            }
        }.start();
    }

    public void setContent(String replace, String s) {
        if (null != mContent) {
            mContent.setText(replace);
            mTtitle.setText(s);
            refreshLayout.finishRefresh();
        }
    }

    UDetailsActivity activity;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        activity = (UDetailsActivity) context;
    }

    @Override
    protected void initData() {
//        this.getBanner().loadAD();
    }

//    BannerView bv;

    public DetailsListFragment() {
        // Required empty public constructor
    }


    @Override
    public void fetchData() {
        refreshLayout.autoRefresh();
    }
}
